/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/*
 * Consultar.java
 *
 * Created on 02/11/2011, 12:00:14
 */

package Interfaces;

import classes.Palestrante;
import classes.Recurso;
import conexaoBD.mySQLcrud;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Kazuo
 */
public class TelaAdicionarPalestrantes extends javax.swing.JFrame {

    private ArrayList<Palestrante> arrayListPalestrante;
    private mySQLcrud connectionCrud;
    private TelaCadastroMiniCurso formularioPai;

    /** Creates new form Consultar */
    public TelaAdicionarPalestrantes() {
        initComponents();
         connectionCrud = new mySQLcrud();
        arrayListPalestrante = connectionCrud.listaPalestrante("");
        preencheComboBoxRecurso();
    }

    public void setFormilario(TelaCadastroMiniCurso formPai) {
        formularioPai = formPai;
    }


    private void preencheComboBoxRecurso() {

        DefaultComboBoxModel modelListaRecursos = new DefaultComboBoxModel();
        modelListaRecursos.addElement("Selecione a instituição do palestrante");
        for (int i = 0; i < arrayListPalestrante.size(); i++) {
            String tipoRecurso = arrayListPalestrante.get(i).getInstituicaoPalestrante();
            if (modelListaRecursos.getIndexOf(tipoRecurso) == -1) {
                modelListaRecursos.addElement(tipoRecurso);
            }
        }
        cbSelecionaInstituicao.setModel(modelListaRecursos);
    }


    private void preecheListaRecursos(String itemSelecionado) {
        jListPalestrantes.removeAll();
        DefaultListModel modelListaRecursos = new DefaultListModel();

        for (int i = 0; i < arrayListPalestrante.size(); i++) {
            String intituicaoPalestrante = arrayListPalestrante.get(i).getInstituicaoPalestrante();
            if (intituicaoPalestrante.equals(itemSelecionado)) {
                modelListaRecursos.addElement(arrayListPalestrante.get(i).getNomePalestrante());
            }
        }

        jListPalestrantes.setModel(modelListaRecursos);

    }





    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        lblTitulo = new javax.swing.JLabel();
        lblParamConsulta = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btConfirmar = new javax.swing.JButton();
        btAdiciona = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListPalestrantes = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListPalestrantesAdicionados = new javax.swing.JList();
        btCancelar1 = new javax.swing.JButton();
        btRemove = new javax.swing.JButton();
        cbSelecionaInstituicao = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblTitulo.setFont(new java.awt.Font("Tahoma", 1, 14));
        lblTitulo.setText("Adicionar");
        jPanel1.add(lblTitulo);

        lblParamConsulta.setFont(new java.awt.Font("Tahoma", 1, 16));
        lblParamConsulta.setText("Palestrantes");
        jPanel1.add(lblParamConsulta);

        jPanel2.setLayout(new java.awt.GridBagLayout());

        btConfirmar.setText("Confirmar");
        btConfirmar.setToolTipText("Confirmar Cadastro de Sessão");
        btConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConfirmarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(34, 0, 0, 0);
        jPanel2.add(btConfirmar, gridBagConstraints);

        btAdiciona.setText("Adiciona >>");
        btAdiciona.setToolTipText("Cancelar e retornar para a tela anterior");
        btAdiciona.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAdicionaActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        jPanel2.add(btAdiciona, gridBagConstraints);

        jListPalestrantes.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { " ", " ", " ", " ", " ", " " };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jListPalestrantes);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.ipadx = 220;
        gridBagConstraints.ipady = 120;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        jPanel2.add(jScrollPane1, gridBagConstraints);

        jScrollPane2.setViewportView(jListPalestrantesAdicionados);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 4;
        gridBagConstraints.ipadx = 220;
        gridBagConstraints.ipady = 120;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel2.add(jScrollPane2, gridBagConstraints);

        btCancelar1.setText("Cancelar");
        btCancelar1.setToolTipText("Cancelar e retornar para a tela anterior");
        btCancelar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelar1ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(34, 0, 0, 0);
        jPanel2.add(btCancelar1, gridBagConstraints);

        btRemove.setText("<< Remove");
        btRemove.setToolTipText("Cancelar e retornar para a tela anterior");
        btRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 3;
        jPanel2.add(btRemove, gridBagConstraints);

        cbSelecionaInstituicao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione o tipo do" }));
        cbSelecionaInstituicao.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbSelecionaInstituicaoItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        jPanel2.add(cbSelecionaInstituicao, gridBagConstraints);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 572, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConfirmarActionPerformed
        ArrayList<String> listaRecursosString = new ArrayList<String>();
        ArrayList<Palestrante> listaPalestranteObj = new ArrayList<Palestrante>();


        for (int i = 0; i < jListPalestrantesAdicionados.getModel().getSize(); i++) {
            listaRecursosString.add((String) jListPalestrantesAdicionados.getModel().getElementAt(i));
        }

        for (Palestrante p : arrayListPalestrante) {
            if (listaRecursosString.contains(p.getNomePalestrante())) {
                listaPalestranteObj.add(p);
            }
        }

        formularioPai.setListaPalestranteSelecionados(listaPalestranteObj);
        formularioPai = null;

        this.dispose();
    }//GEN-LAST:event_btConfirmarActionPerformed

    private void btAdicionaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAdicionaActionPerformed
        Object[] objItensSelecionados = jListPalestrantes.getSelectedValues();
        ArrayList<String> itemNaoSelecionado = new ArrayList<String>();
        ArrayList<String> itemSelecionado = new ArrayList<String>();
        DefaultListModel listaTotal = new DefaultListModel();

        if (objItensSelecionados.length >0) {
            for (int i = 0; i < jListPalestrantes.getModel().getSize(); i++) {
                itemNaoSelecionado.add((String) jListPalestrantes.getModel().getElementAt(i));
            }


            for (int i = 0; i < jListPalestrantesAdicionados.getModel().getSize(); i++) {
                itemSelecionado.add((String) jListPalestrantesAdicionados.getModel().getElementAt(i));
            }

            if (objItensSelecionados.length > 0) {
                for (int i = 0; i < objItensSelecionados.length; i++) {
                    if (!itemSelecionado.contains((String) objItensSelecionados[i])) {
                        itemSelecionado.add((String) objItensSelecionados[i]);
                    } else {
                        JOptionPane.showMessageDialog(null, "O palestrante '" + (String) objItensSelecionados[i] + "' já foi adicionado a lista de palestrantes selecionados");
                    }
                }
            } else {
                if (!itemSelecionado.contains((String) objItensSelecionados[0])) {
                    itemSelecionado.add((String) objItensSelecionados[0]);
                } else {
                    JOptionPane.showMessageDialog(null, "O palestrante '" + (String) objItensSelecionados[0] + "' já foi adicionado a lista de palestrante selecionados");
                }
            }


            for (String s : itemSelecionado) {
                listaTotal.addElement(s);
            }

            jListPalestrantesAdicionados.setModel(listaTotal);
        } else {
            JOptionPane.showMessageDialog(null, "Você deve selecionar algum recurso para adiciona-lo");
        }
}//GEN-LAST:event_btAdicionaActionPerformed

    private void btRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoveActionPerformed
        Object[] objListaRecursosAdicionadosSelecionado = jListPalestrantesAdicionados.getSelectedValues();
        ArrayList<String> listaTodosNomes = new ArrayList<String>();
        ArrayList<String> arrayRecursosAdicionadosSelecionado = new ArrayList<String>();
        ArrayList<String> interseccaoRecursosAdicionadosRemovido = new ArrayList<String>();

        if(objListaRecursosAdicionadosSelecionado.length >0){

        // tranforma os elementos de Object[] p/ arrayList<string>
        for (Object o : objListaRecursosAdicionadosSelecionado) {
            arrayRecursosAdicionadosSelecionado.add((String) o);
        }

        // lista todos os nomes dos recursos
        for (int i = 0; i < jListPalestrantesAdicionados.getModel().getSize(); i++) {
            listaTodosNomes.add((String) jListPalestrantesAdicionados.getModel().getElementAt(i));
        }

        // compara e add
        for (String s : listaTodosNomes) {
            if (!arrayRecursosAdicionadosSelecionado.contains(s)) {
                interseccaoRecursosAdicionadosRemovido.add(s);
            }
        }

        DefaultListModel modelList = new DefaultListModel();

        for (String s : interseccaoRecursosAdicionadosRemovido) {
            modelList.addElement(s);
        }

        jListPalestrantesAdicionados.setModel(modelList);
        }else{
            JOptionPane.showMessageDialog(null, "Você deve selecionar algum recurso para remove-lo");
        }

    }//GEN-LAST:event_btRemoveActionPerformed

    private void btCancelar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelar1ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btCancelar1ActionPerformed

    private void cbSelecionaInstituicaoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSelecionaInstituicaoItemStateChanged

        String itemSelecionado = (String) cbSelecionaInstituicao.getSelectedItem();
        if (!itemSelecionado.equals("Selecione a instituição do palestrante")) {
            preecheListaRecursos(itemSelecionado);
        }
}//GEN-LAST:event_cbSelecionaInstituicaoItemStateChanged

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TelaConsulta().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdiciona;
    private javax.swing.JButton btCancelar1;
    private javax.swing.JButton btConfirmar;
    private javax.swing.JButton btRemove;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbSelecionaInstituicao;
    private javax.swing.JList jListPalestrantes;
    private javax.swing.JList jListPalestrantesAdicionados;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblParamConsulta;
    private javax.swing.JLabel lblTitulo;
    // End of variables declaration//GEN-END:variables

}
